
using IrisGenerator.Common.DataTypes;
using IrisGenerator.Common.Modules;
using IrisGenerator.Common.Tools;
using System.ComponentModel;
using IrisGenerator.Framework.RuleEngine;
using IrisGenerator.Framework.Events;
namespace IrisGenerator.Common.Core
{


    public partial class Target : BindableObject<Target>, IRuleable<Target>, INotifyPropertyChanged
    {

        private System.Nullable<System.Guid> _tarId;

        private string _tarCode;

        private string _tarDescription;

        private MetatypeEquivalencesCollection _metatypeEquivalencesCollection;

        private ModuleCollection _moduleCollection;

        public virtual System.Nullable<System.Guid> TarId
        {
            get
            {
                return _tarId;
            }
            set
            {
                _tarId = value;
                this.OnPropertyChanged("TarId");
            }
        }

        public virtual string TarCode
        {
            get
            {
                return _tarCode;
            }
            set
            {
                _tarCode = value;
                this.OnPropertyChanged("TarCode");
            }
        }

        public virtual string TarDescription
        {
            get
            {
                return _tarDescription;
            }
            set
            {
                _tarDescription = value;
                this.OnPropertyChanged("TarDescription");
            }
        }

        public virtual MetatypeEquivalencesCollection MetatypeEquivalencesCollection
        {
            get
            {
                if ((this._metatypeEquivalencesCollection == null))
                {
                    _metatypeEquivalencesCollection = MetatypeEquivalences.Select_METATYPE_EQUIVALENCES_By_TAR_ID(this.TarId);
                }
                return this._metatypeEquivalencesCollection;
            }
        }

        public virtual ModuleCollection ModuleCollection
        {
            get
            {
                if ((this._moduleCollection == null))
                {
                    _moduleCollection = Module.Select_MODULE_By_TAR_ID(this.TarId);
                }
                return this._moduleCollection;
            }
        }

        private void Clean()
        {
            this.TarId = null;
            this.TarCode = string.Empty;
            this.TarDescription = string.Empty;
            this._metatypeEquivalencesCollection = null;
            this._moduleCollection = null;
        }

        private void Fill(System.Data.DataRow dr)
        {
            this.Clean();
            if ((dr["TAR_ID"] != System.DBNull.Value))
            {
                this.TarId = ((System.Nullable<System.Guid>)(dr["TAR_ID"]));
            }
            if ((dr["TAR_CODE"] != System.DBNull.Value))
            {
                this.TarCode = ((string)(dr["TAR_CODE"]));
            }
            if ((dr["TAR_DESCRIPTION"] != System.DBNull.Value))
            {
                this.TarDescription = ((string)(dr["TAR_DESCRIPTION"]));
            }
        }

        public static TargetCollection GetAll()
        {
            IrisGenerator.Dal.Target dbo = null;
            try
            {
                dbo = new IrisGenerator.Dal.Target();
                System.Data.DataSet ds = dbo.TARGET_Select_All();
                TargetCollection collection = new TargetCollection();
                if (GlobalTools.IsSafeDataSet(ds))
                {
                    for (int i = 0; (i < ds.Tables[0].Rows.Count); i = (i + 1))
                    {
                        Target obj = new Target();
                        obj.Fill(ds.Tables[0].Rows[i]);
                        if ((obj != null))
                        {
                            collection.Add(obj);
                        }
                    }
                }
                return collection;
            }
            catch (System.Exception)
            {
                throw;
            }
            finally
            {
                if ((dbo != null))
                {
                    dbo.Dispose();
                }
            }
        }

        public static Target Load(System.Nullable<System.Guid> TAR_ID)
        {
            IrisGenerator.Dal.Target dbo = null;
            try
            {
                dbo = new IrisGenerator.Dal.Target();
                System.Data.DataSet ds = dbo.TARGET_Select_One(TAR_ID);
                Target obj = null;
                if (GlobalTools.IsSafeDataSet(ds))
                {
                    if ((ds.Tables[0].Rows.Count > 0))
                    {
                        obj = new Target();
                        obj.Fill(ds.Tables[0].Rows[0]);
                    }
                }
                return obj;
            }
            catch (System.Exception)
            {
                throw;
            }
            finally
            {
                if ((dbo != null))
                {
                    dbo.Dispose();
                }
            }
        }

        public virtual void Load()
        {
            IrisGenerator.Dal.Target dbo = null;
            try
            {
                dbo = new IrisGenerator.Dal.Target();
                System.Data.DataSet ds = dbo.TARGET_Select_One(this.TarId);
                if (GlobalTools.IsSafeDataSet(ds))
                {
                    if ((ds.Tables[0].Rows.Count > 0))
                    {
                        this.Fill(ds.Tables[0].Rows[0]);
                    }
                }
            }
            catch (System.Exception)
            {
                throw;
            }
            finally
            {
                if ((dbo != null))
                {
                    dbo.Dispose();
                }
            }
        }

        public override void Insert()
        {
            IrisGenerator.Dal.Target dbo = null;
            try
            {
                dbo = new IrisGenerator.Dal.Target();
                dbo.TARGET_Insert(this.TarCode, this.TarDescription);
            }
            catch (System.Exception)
            {
                throw;
            }
            finally
            {
                if ((dbo != null))
                {
                    dbo.Dispose();
                }
            }
        }

        public override bool Delete(bool fireRuleMessages)
        {
            RuleErrorCollection errors=null;
            if (this.ObjectRules.CanDelete(ref errors))
            {
                IrisGenerator.Dal.Target dbo = null;
                try
                {
                    dbo = new IrisGenerator.Dal.Target();
                    BaseEventArgs<Target> e = new BaseEventArgs<Target>(this, fireRuleMessages);
                    this.OnDeleting(e);
                    if (!e.Cancel)
                    {
                        dbo.TARGET_Delete(this.TarId);
                        return true;
                    }
                    else
                        return false;
                }
                catch (System.Exception ex)
                {
                    throw;
                }
                finally
                {
                    if ((dbo != null))
                    {
                        dbo.Dispose();
                    }
                }
            }
            else
            {
                //TODO: Traitement des erreurs...
                return false;
            }
        }

        public override void Update()
        {
            IrisGenerator.Dal.Target dbo = null;
            try
            {
                dbo = new IrisGenerator.Dal.Target();
                dbo.TARGET_Update(this.TarId, this.TarCode, this.TarDescription);
            }
            catch (System.Exception)
            {
                throw;
            }
            finally
            {
                if ((dbo != null))
                {
                    dbo.Dispose();
                }
            }
        }
    }
}
